﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MacomberMapSystem.Common.Database;
using System.Drawing;
using MacomberMapSystem.Common.CIM.Direct;

namespace MacomberMapSystem
{
    /// <summary>
    /// (C) 2012, Electric Reliability Council of Texas, Inc.
    /// This class provides a tool strip status label to highlight the model load state
    /// </summary>
    public class ModelLabel : ToolStripStatusLabel
    {
        /// <summary>The model associated with our label</summary>
        public MM_Database_Model Model;

        /// <summary>Our last state</summary>
        public MM_Database_Model.enumModelState LastState = MM_Database_Model.enumModelState.Unopened;

        /// <summary>
        /// Initialize a new model label
        /// </summary>
        /// <param name="Model"></param>
        /// <param name="MouseUpHandler"></param>
        /// <param name="ssMain"></param>
        public ModelLabel(MM_Database_Model Model, MouseEventHandler MouseUpHandler, StatusStrip ssMain)
        {
            this.Model = Model;
            this.MouseUp += MouseUpHandler;
            this.Text = (String.IsNullOrEmpty(Model.FullClass) ? Model.Name : Model.FullClass) + (Model.State == MM_Database_Model.enumModelState.Completed ? "" : ": " + Model.State.ToString().Replace('_', ' '));
            if (Model is CIM_DirectModel)
                this.Font = new Font(this.Font, FontStyle.Bold);
            LastState = Model.State;
            ssMain.Items.Add(this);

        }

        /// <summary>
        /// Paint our label with the appropriate text
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                //First, update our text if needed
                if (LastState != Model.State)
                {
                    LastState = Model.State;
                    this.Text = (String.IsNullOrEmpty(Model.FullClass) ? Model.Name : Model.FullClass) + (Model.State == MM_Database_Model.enumModelState.Completed ? "" : ": " + Model.State.ToString().Replace('_', ' '));
                }

                //First, determine our background color
                Color BackColor = Color.Yellow;
                Color ForeColor = Color.Black;
                if (Model.State == MM_Database_Model.enumModelState.Completed)
                    BackColor = Color.LightGreen;
                else if (Model.State == MM_Database_Model.enumModelState.Unopened || Model.State == MM_Database_Model.enumModelState.Error)
                    BackColor = Color.Red;
                else if (Model.State == MM_Database_Model.enumModelState.Open_Commencing)
                    BackColor = Color.GreenYellow;

                RectangleF DrawRect = new RectangleF(0f, 0f, (Model.Current / Model.Total) * this.Width, this.Height);
                if (Model.State == MM_Database_Model.enumModelState.Error)
                    DrawRect.Width = this.Width;
                if (!float.IsNaN(DrawRect.Width) && DrawRect.Width > 0)
                    //if ( || Model.State == MM_Database_Model.enumModelState.Completed || Model.State == MM_Database_Model.enumModelState.Error)
                        using (Brush sB = new SolidBrush(BackColor))
                            e.Graphics.FillRectangle(sB, DrawRect);
                    /*else
                        using (Brush sB = new System.Drawing.Drawing2D.LinearGradientBrush(DrawRect, BackColor, this.BackColor, 0f))
                            e.Graphics.FillRectangle(sB, DrawRect);
                */
                ControlPaint.DrawBorder3D(e.Graphics, 0, 0, this.Width, this.Height, Border3DStyle.Flat);
                using (Brush ForeBrush = new SolidBrush(ForeColor))
                    e.Graphics.DrawString(this.Text, this.Font, ForeBrush, new RectangleF(0, 0, this.Width, this.Height), MacomberMapSystem.Common.User_Interfaces.One_Lines.MM_OneLine_Element.CenterFormat);
            }
            catch
            {
            }
        }
    }
}
